package com.harmontronics.hanma.paas.paas.domain.organization.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.valueobject.HmUserOrgFields;
import com.harmontronics.hanma.paas.paas.domain.organization.repository.persistence.HmUserOrgSerialRepository;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.HmUserOrgSerial;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.organization.repository.mapper.HmUserOrgSerialMapper;
import com.harmontronics.hanma.paas.paas.domain.organization.repository.po.HmUserOrgSerialPO;
import java.util.List;

@Service("hmUserOrgSerialService")
@DS("#header.App-Unique")
public class HmUserOrgSerialService{

    @Autowired
    private HmUserOrgSerialRepository hmUserOrgSerialRepository;

    @Autowired
    private HmUserOrgSerialMapper hmUserOrgSerialMapper;


    public HmUserOrgSerial findById( Integer id){
            HmUserOrgSerialPO po =   hmUserOrgSerialRepository.getById(id);
        return BeanUtil.toBean(po,HmUserOrgSerial.class);
    }

    public void save(HmUserOrgSerial hmUserOrgSerial){
            HmUserOrgSerialPO po =  BeanUtil.toBean(hmUserOrgSerial,HmUserOrgSerialPO.class);
            hmUserOrgSerialRepository.save(po);
    }

    public void update(HmUserOrgSerial hmUserOrgSerial){
            HmUserOrgSerialPO po =  BeanUtil.toBean(hmUserOrgSerial,HmUserOrgSerialPO.class);
            hmUserOrgSerialRepository.updateById(po);
    }

    public void remove(List<Integer> ids){
            hmUserOrgSerialRepository.removeByIds(ids);
    }

    public List<HmUserOrgSerial> getList(){
        List<HmUserOrgSerialPO> pos =  hmUserOrgSerialRepository.list();
        return Convert.toList(HmUserOrgSerial.class,pos) ;
    }

    public PageBase<HmUserOrgSerial> getList(int pageNo,int pageSize){
        Page<HmUserOrgSerialPO> page = new Page<>(pageNo, pageSize);
        IPage<HmUserOrgSerialPO> resultPage =
                    hmUserOrgSerialRepository.selectPage(Wrappers.emptyWrapper(),page);
        return PageBase.from(resultPage,HmUserOrgSerial.class);
    }

    public void userSort(Long userId, Long serialNum, Long orgId) {
        QueryWrapper<HmUserOrgSerialPO> queryWrapper =
                new QueryWrapper<HmUserOrgSerialPO>()
                        .eq(HmUserOrgFields.USER_ID, userId)
                        .eq(HmUserOrgFields.ORG_ID, orgId);
        HmUserOrgSerialPO orgSerial = hmUserOrgSerialMapper.selectOne(queryWrapper);
        if (orgSerial == null) {
            orgSerial = new HmUserOrgSerialPO();
            orgSerial.setUserId(userId);
            orgSerial.setOrgId(orgId);
            orgSerial.setSerialNum(serialNum);
            orgSerial.setDeleted(0);
            hmUserOrgSerialRepository.save(orgSerial);
        } else {
            orgSerial.setSerialNum(serialNum);
            hmUserOrgSerialMapper.update(orgSerial, queryWrapper);
        }
    }

}